home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmassoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  2.8 KB  |  106 lines

  1. // CmAssoc.cpp
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // Association implementation.
  7. // -----------------------------------------------------------------
  8.  
  9. #include <cm/include/cmassoc.h>
  10.  
  11.  
  12. // "CmAssociation" is the association constructor.
  13. //
  14. CmAssociation::CmAssociation(CmObject* K, CmObject* O)
  15.                              : _key(K), _object(O)
  16. {}
  17.  
  18.  
  19. // "CmAssociation" is the association copy constructor.
  20. //
  21. CmAssociation::CmAssociation(const CmAssociation& A)
  22. {
  23.   _key    = (A._key)    ? A._key->newCopy()    : NULL;
  24.   _object = (A._object) ? A._object->newCopy() : NULL;
  25. }
  26.  
  27.  
  28. // "=" assignment operator copies the contents of the specified
  29. // assocation into this assocation.
  30. //
  31. CmAssociation& CmAssociation::operator=(const CmAssociation& A)
  32. {
  33.   if (&A != this)
  34.   {
  35.     _key    = (A._key)    ? A._key->newCopy()    : NULL;
  36.     _object = (A._object) ? A._object->newCopy() : NULL;
  37.   }
  38.   return *this;
  39. }
  40.  
  41.  
  42. // "isEqual" returns whether or not the specified object is equal to
  43. // this object.
  44. //
  45. Bool CmAssociation::isEqual(CmObject* pObj) const
  46. {
  47.   if (!pObj->isA("CmAssociation")) return CmObject::isEqual(pObj);
  48.   CmAssociation* pAss = (CmAssociation*) pObj;
  49.   return (_key) ? _key->isEqual(pAss->_key) : FALSE;
  50. }
  51.  
  52.  
  53. // "compare" compares the specified object to this object.
  54. //
  55. int CmAssociation::compare(CmObject* pObj) const
  56. {
  57.   if (!pObj->isA("CmAssociation")) return CmObject::compare(pObj);
  58.   CmAssociation* pAss = (CmAssociation*) pObj;
  59.   return (_key) ? _key->compare(pAss->_key) : -1;
  60. }
  61.  
  62.  
  63. // "hash" returns a hash value based on the input parameter.
  64. //
  65. unsigned CmAssociation::hash(unsigned m) const
  66. {
  67.   return (_key) ? _key->hash(m) : CmObject::hash(m);
  68. }
  69.  
  70.  
  71. // "printOn" prints this association on the specified output stream.
  72. //
  73. void CmAssociation::printOn(ostream& os) const
  74. {
  75.   if (_key && _object)
  76.     os << "(" << *_key << ", " << *_object << ")";
  77. }
  78.  
  79.  
  80. // "write" writes the association objects to the specified file.
  81. //
  82. Bool CmAssociation::write(CmReserveFile& file) const
  83. {
  84.   if (!_key || !_object) return FALSE;
  85.   Bool success = _key->writeObject(file);            // Write key.
  86.   if (success) success = _object->writeObject(file); // Write object.
  87.   return success;
  88. }
  89.  
  90.  
  91. // "read" reads association objects from the specified file.
  92. //
  93. Bool CmAssociation::read(CmReserveFile& file)
  94. {
  95.   _key = _object = NULL;
  96.   _key = CmObject::readObject(file);                 // Read key object.
  97.   if (!_key) return FALSE;
  98.  
  99.   _object = CmObject::readObject(file);              // Read value object.
  100.   if (_object) return TRUE;
  101.  
  102.   delete _key;
  103.   _key = NULL;
  104.   return FALSE;
  105. }
  106.